﻿using System.Collections.Generic;
using System.Windows.Forms;
using ApplicationLibrary.ExtendedControls;
using ApplicationLibrary.SqlConnector;

namespace Plumber.Masters.ModuleData
{

    internal class RadarCompanyParam : RadarParameters
    {
        public RadarCompanyParam(int code)
        {
            Params = new List<object>();
            Params.Add(code);
        }
    }

    internal class RadarMasterParam : RadarParameters
    {
        public RadarMasterParam(string type, string code)
        {
            Params = new List<object>();
            Params.Add(code);
            Params.Add(type);
        }
    }

    internal class RadarTitlesParam : RadarParameters
    {
        public RadarTitlesParam(string code)
        {
            Params = new List<object>();
            Params.Add(code);
        }
    }

    internal class RadarMasterTypesParam : RadarParameters
    {
        public RadarMasterTypesParam(string code)
        {
            Params = new List<object>();
            Params.Add(code);
        }
    }

    internal class RadarTaxCodesParam : RadarParameters
    {
        public RadarTaxCodesParam(string code)
        {
            Params = new List<object>();
            Params.Add(code);
        }
    }

    internal class RadarDescriptionsParam : RadarParameters
    {
        public RadarDescriptionsParam(DescriptionType type, string code)
        {
            Params = new List<object>();
            Params.Add(type);
            Params.Add(code);
        }
    }

    #region Radar RadarMasterForm

    internal class RadarMasterForm : RadarForm
    {
        private QueryBuilder qb = new QueryBuilder();
        private SqlABParameter p1, p2, p3, p4;

        public enum Module { All, Master, Accounting };

        public bool DecodeAddress { private get; set; }

        private Module module;
        private DRMasterTypeByType drMasterTypeByType = null;
        private string condominium = string.Empty;

        public RadarMasterForm(string type, Module module)
            : this(module)
        {
            this.FilterType = type;
            drMasterTypeByType = new DRMasterTypeByType();
            if (drMasterTypeByType.Find(MasterTypes.E_Condominium))
                condominium = drMasterTypeByType.GetValue<string>(PL_MasterType.Code);
        }

        public Module ModuleType
        {
            set
            {
                module = value;
                rdrNameSpace = module == Module.Accounting
                                  ? "Plumber.Plumber.Accounting.Forms.AccountingMasters"
                                  : "Plumber.Plumber.Masters.Forms.Masters";
            }

            get { return module; }
        }

        public RadarMasterForm(Module module)
            : base()
        {
            rdrCodeColumn = PL_Masters.Code.Name;
            rdrDescColumn = PL_Masters.Description.Name;
            this.ModuleType = module;
        }

        protected override Table GetRadarTable()
        {
            return new PL_Masters();
        }

        protected override bool DefineFindQuery(SqlABCommand sqlCmd)
        {
            p1 = new SqlABParameter("@p1", PL_Masters.Code);
            p2 = new SqlABParameter("@p2", PL_Masters.Type);

            qb.Clear();
            qb.AddSelectAll(PL_Masters.Name);
            qb.AddFrom(PL_Masters.Name);
            qb.AddCompare(PL_Masters.Code, p1);
            qb.AddCompare(PL_Masters.Type, p2);

            sqlCmd.CommandText = qb.Query;
            sqlCmd.Parameters.Add(p1);
            sqlCmd.Parameters.Add(p2);

            return true;
        }

        protected override void PrepareFindQuery(IRadarParameters param)
        {
            p1.Value = param[0];
            p2.Value = param[1];
        }

        protected override void OnFound(SqlABDataReader sqlReader)
        {
            if (!sqlReader.GetValue<bool>(PL_Masters.IsAccountingType) &&
                !string.IsNullOrEmpty(condominium) &&
                DecodeAddress &&
                sqlReader.GetValue<string>(PL_Masters.Type) == condominium)
                Description = sqlReader.GetValue<string>(PL_Masters.Address);
            else
                Description = sqlReader[PL_Masters.Description.Name].ToString();
        }

        protected override string DefineBrowseQuery(SqlABCommand sqlCmd, string findQuery)
        {
            p3 = new SqlABParameter("@p3", PL_Masters.Disabled);
            qb.Clear();
            qb.AddSelectAll(PL_Masters.Name);
            qb.AddFrom(PL_Masters.Name);
            qb.AddFilter(findQuery);
            qb.AddCompare(PL_Masters.Disabled, p3);
            switch (module)
            {
                case Module.Accounting:
                    qb.AddCompare<bool>(PL_Masters.IsAccountingType, true);
                    break;

                case Module.Master:
                    qb.AddCompare<bool>(PL_Masters.IsAccountingType, false);
                    break;
            }

            if (!string.IsNullOrEmpty(FilterType))
            {
                p4 = new SqlABParameter("@p4", PL_Masters.Type);
                qb.AddCompare(PL_Masters.Type, p4);
                sqlCmd.Parameters.Add(p4);
                p4.Value = FilterType;
            }

            sqlCmd.Parameters.Add(p3);

            return qb.Query;
        }

        protected override void PrepareBrowseQuery()
        {
            p3.Value = 0;
        }

        protected override IRadarParameters PrepareRadarParameters(DataGridViewRow row)
        {
            string mt = DataGridViewRowValues.GetValue<string>(row, PL_Masters.Type);
            string Code = DataGridViewRowValues.GetValue<string>(row, PL_Masters.Code);
            return new RadarMasterParam(mt, Code);
        }

        public override IRadarParameters GetRadarParameters(string text)
        {
            return new RadarMasterParam(FilterType, text);
        }

        public override string GetCodeFromParameters(IRadarParameters param)
        {
            return param.GetValue<string>(0);
        }
    }

    #endregion

    #region Radar RadarCompanyForm

    internal class RadarCompanyForm : RadarForm
    {
        private QueryBuilder qb = new QueryBuilder();
        private SqlABParameter p1;

        public bool DecodeAddress { private get; set; }

        public RadarCompanyForm()
            : base()
        {
        }

        protected override Table GetRadarTable()
        {
            return new PL_Company();
        }

        protected override bool DefineFindQuery(SqlABCommand sqlCmd)
        {
            p1 = new SqlABParameter("@p1", PL_Company.Code);

            qb.Clear();
            qb.AddSelectAll(PL_Company.Name);
            qb.AddFrom(PL_Company.Name);
            qb.AddCompare(PL_Company.Code, p1);

            sqlCmd.CommandText = qb.Query;
            sqlCmd.Parameters.Add(p1);

            return true;
        }

        protected override void PrepareFindQuery(IRadarParameters param)
        {
            p1.Value = param[0];
        }

        protected override void OnFound(SqlABDataReader sqlReader)
        {
            Description = sqlReader[PL_Company.Description.Name].ToString();
        }

        protected override string DefineBrowseQuery(SqlABCommand sqlCmd, string findQuery)
        {
            qb.Clear();
            qb.AddSelectAll(PL_Company.Name);
            qb.AddFrom(PL_Company.Name);
            qb.AddFilter(findQuery);

            return qb.Query;
        }

        protected override void PrepareBrowseQuery()
        {
        }

        protected override IRadarParameters PrepareRadarParameters(DataGridViewRow row)
        {
            return new RadarCompanyParam(1);
        }

        public override IRadarParameters GetRadarParameters(string text)
        {
            return new RadarCompanyParam(1);
        }

        public override string GetCodeFromParameters(IRadarParameters param)
        {
            return param.GetValue<string>(1);
        }
    }

    #endregion

    #region Radar Titles

    internal class RadarTitles : RadarForm
    {
        private QueryBuilder qb = new QueryBuilder();
        private SqlABParameter p1;
        private SqlABParameter p2;

        public RadarTitles()
            : base()
        {
            rdrCodeColumn = PL_Titles.Title.Name;
            rdrDescColumn = PL_Titles.Description.Name;
            rdrNameSpace = "Plumber.Plumber.Masters.Forms.Titles";
        }

        protected override Table GetRadarTable()
        {
            return new PL_Titles();
        }

        protected override bool DefineFindQuery(SqlABCommand sqlCmd)
        {
            qb.Clear();
            qb.AddSelectAll(PL_Titles.Name);
            qb.AddFrom(PL_Titles.Name);
            qb.AddCompare(PL_Titles.Title, "@p1");

            p1 = new SqlABParameter("@p1", PL_Titles.Title);

            sqlCmd.CommandText = qb.Query;
            sqlCmd.Parameters.Add(p1);

            return true;
        }

        protected override void PrepareFindQuery(IRadarParameters param)
        {
            p1.Value = param.Params[0];
        }

        protected override void OnFound(SqlABDataReader sqlReader)
        {
            Description = sqlReader[PL_Titles.Description.Name].ToString();
        }

        protected override string DefineBrowseQuery(SqlABCommand sqlCmd, string findQuery)
        {
            p2 = new SqlABParameter("@p2", PL_Titles.Title);

            qb.Clear();
            qb.AddSelectAll(PL_Titles.Name);
            qb.AddFrom(PL_Titles.Name);
            qb.AddFilter(findQuery);

            sqlCmd.Parameters.Add(p2);

            return qb.Query;
        }

        protected override void PrepareBrowseQuery()
        {
            p2.Value = PL_Titles.Title.DefaultValue;
        }

        protected override IRadarParameters PrepareRadarParameters(DataGridViewRow row)
        {
            return new RadarTitlesParam(DataGridViewRowValues.GetValue<string>(row, PL_Titles.Title));
        }

        public override IRadarParameters GetRadarParameters(string text)
        {
            return new RadarTitlesParam(text);
        }

        public override string GetCodeFromParameters(IRadarParameters param)
        {
            return param.GetValue<string>(0);
        }
    }

    #endregion

    #region Radar MasterTypes

    internal class RadarMasterTypes : RadarForm
    {
        private QueryBuilder qb = new QueryBuilder();
        private SqlABParameter p1;
        private SqlABParameter p2;
        private bool isAccounting;

        public RadarMasterTypes(bool IsAccounting)
            : base()
        {
            rdrCodeColumn = PL_MasterType.Code.Name;
            rdrDescColumn = PL_MasterType.Description.Name;
            rdrNameSpace = IsAccounting
                            ? "Plumber.Plumber.Accouning.Forms.AccountingType"
                            : "Plumber.Plumber.Masters.Forms.MasterType";
            isAccounting = IsAccounting;
        }

        protected override Table GetRadarTable()
        {
            return new PL_MasterType();
        }

        protected override bool DefineFindQuery(SqlABCommand sqlCmd)
        {
            p1 = new SqlABParameter("@p1", PL_MasterType.Code);

            qb.Clear();
            qb.AddSelectAll(PL_MasterType.Name);
            qb.AddFrom(PL_MasterType.Name);
            qb.AddCompare(PL_MasterType.Code, p1);

            sqlCmd.CommandText = qb.Query;
            sqlCmd.Parameters.Add(p1);

            return true;
        }

        protected override void PrepareFindQuery(IRadarParameters param)
        {
            p1.Value = param[0];
        }

        protected override void OnFound(SqlABDataReader sqlReader)
        {
            Description = sqlReader[PL_MasterType.Description.Name].ToString();
        }

        protected override string DefineBrowseQuery(SqlABCommand sqlCmd, string findQuery)
        {
            p2 = new SqlABParameter("@p2", PL_MasterType.Code);

            qb.Clear();
            qb.AddSelectAll(PL_MasterType.Name);
            qb.AddFrom(PL_MasterType.Name);
            qb.AddCompare<bool>(PL_MasterType.IsAccountingType, isAccounting);
            qb.AddFilter(findQuery);

            sqlCmd.Parameters.Add(p2);

            return qb.Query;
        }

        protected override void PrepareBrowseQuery()
        {
            p2.Value = PL_MasterType.Code.DefaultValue;
        }

        protected override IRadarParameters PrepareRadarParameters(DataGridViewRow row)
        {
            return new RadarMasterTypesParam(DataGridViewRowValues.GetValue<string>(row, PL_MasterType.Code));
        }

        public override IRadarParameters GetRadarParameters(string text)
        {
            return new RadarMasterTypesParam(text);
        }

        public override string GetCodeFromParameters(IRadarParameters param)
        {
            return param.GetValue<string>(0);
        }
    }

    #endregion

    #region Radar TaxCodes

    internal class RadarTaxCodes : RadarForm
    {
        private QueryBuilder qb = new QueryBuilder();
        private SqlABParameter p1;
        private SqlABParameter p2;

        public RadarTaxCodes()
            : base()
        {
            rdrCodeColumn = PL_TaxCode.Code.Name;
            rdrDescColumn = PL_TaxCode.Description.Name;
            rdrNameSpace = "Plumber.Plumber.Sales.Forms.TaxCodes";
        }

        protected override Table GetRadarTable()
        {
            return new PL_TaxCode();
        }

        protected override bool DefineFindQuery(SqlABCommand sqlCmd)
        {
            p1 = new SqlABParameter("@p1", PL_TaxCode.Code);

            qb.Clear();
            qb.AddSelectAll(PL_TaxCode.Name);
            qb.AddFrom(PL_TaxCode.Name);
            qb.AddCompare(PL_TaxCode.Code, p1);

            sqlCmd.CommandText = qb.Query;
            sqlCmd.Parameters.Add(p1);

            return true;
        }

        protected override void PrepareFindQuery(IRadarParameters param)
        {
            p1.Value = param[0];
        }

        protected override void OnFound(SqlABDataReader sqlReader)
        {
            Description = sqlReader[PL_TaxCode.Description.Name].ToString();
        }

        protected override string DefineBrowseQuery(SqlABCommand sqlCmd, string findQuery)
        {
            p2 = new SqlABParameter("@p2", PL_TaxCode.Code);
            qb.Clear();
            qb.AddSelectAll(PL_TaxCode.Name);
            qb.AddFrom(PL_TaxCode.Name);
            qb.AddFilter(findQuery);

            return qb.Query;
        }

        protected override void PrepareBrowseQuery()
        {
            p2.Value = PL_TaxCode.Code.DefaultValue;
        }

        protected override IRadarParameters PrepareRadarParameters(DataGridViewRow row)
        {
            return new RadarTaxCodesParam(DataGridViewRowValues.GetValue<string>(row, PL_TaxCode.Code));
        }

        public override IRadarParameters GetRadarParameters(string text)
        {
            return new RadarTaxCodesParam(text);
        }

        public override string GetCodeFromParameters(IRadarParameters param)
        {
            return param.GetValue<string>(0);
        }
    }

    #endregion

    #region Radar Descriptions

    internal class RadarDescriptions : RadarForm
    {
        private QueryBuilder qb = new QueryBuilder();
        private SqlABParameter p1, p2, p3;
        private DescriptionType type;

        public RadarDescriptions(DescriptionType type)
            : base()
        {
            this.type = type;
            rdrCodeColumn = PL_Descriptions.Code.Name;
            rdrDescColumn = PL_Descriptions.Description.Name;
            switch (type)
            {
                case DescriptionType.E_TaxRegister:
                    rdrNameSpace = "Plumber.Plumber.Accounting.Forms.TaxRegisters";
                    break;
            }
        }

        protected override Table GetRadarTable()
        {
            return new PL_Descriptions();
        }

        protected override bool DefineFindQuery(SqlABCommand sqlCmd)
        {
            qb.Clear();
            qb.AddSelectAll(PL_Descriptions.Name);
            qb.AddFrom(PL_Descriptions.Name);
            qb.AddCompare(PL_Descriptions.Type, "@p1");
            qb.AddCompare(PL_Descriptions.Code, "@p2");

            p1 = new SqlABParameter("@p1", PL_Descriptions.Type);
            p2 = new SqlABParameter("@p2", PL_Descriptions.Code);

            sqlCmd.CommandText = qb.Query;
            sqlCmd.Parameters.Add(p1);
            sqlCmd.Parameters.Add(p2);

            return true;
        }

        protected override void PrepareFindQuery(IRadarParameters param)
        {
            p1.Value = param.Params[0];
            p2.Value = param.Params[1];
        }

        protected override void OnFound(SqlABDataReader sqlReader)
        {
            Description = sqlReader[PL_Descriptions.Description.Name].ToString();
        }

        protected override string DefineBrowseQuery(SqlABCommand sqlCmd, string findQuery)
        {
            p3 = new SqlABParameter("@p3", PL_Descriptions.Type);

            qb.Clear();
            qb.AddSelectAll(PL_Descriptions.Name);
            qb.AddFrom(PL_Descriptions.Name);
            qb.AddCompare(PL_Descriptions.Type, "@p3");
            qb.AddFilter(findQuery);

            sqlCmd.Parameters.Add(p3);

            return qb.Query;
        }

        protected override void PrepareBrowseQuery()
        {
            p3.Value = type;
        }

        protected override IRadarParameters PrepareRadarParameters(DataGridViewRow row)
        {
            return new RadarDescriptionsParam(type, DataGridViewRowValues.GetValue<string>(row, PL_Descriptions.Code));
        }

        public override IRadarParameters GetRadarParameters(string text)
        {
            return new RadarDescriptionsParam(type, text);
        }

        public override string GetCodeFromParameters(IRadarParameters param)
        {
            return param.GetValue<string>(1);
        }
    }

    #endregion
}